package de.lazy.gui.components;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;

import javax.swing.JTabbedPane;

class CDropTargetListener implements DropTargetListener {
	
	private DraggableTabbedPane source;
	
	public CDropTargetListener(DraggableTabbedPane source){
		this.source = source;
	}
	

	public void dragEnter(DropTargetDragEvent e) {
		// System.out.println("DropTarget.dragEnter: " + DnDTabbedPane.this);

		if (isDragAcceptable(e)) {
			e.acceptDrag(e.getDropAction());
		} else {
			e.rejectDrag();
		} // if
	}

	public void dragExit(DropTargetEvent e) {
		// System.out.println("DropTarget.dragExit: " + DnDTabbedPane.this);
		source.setDrawRect(false);
	}

	public void dropActionChanged(DropTargetDragEvent e) {
	}

	public void dragOver(final DropTargetDragEvent e) {
		TabTransferData data = source.getTabTransferData(e);

		if (source.getTabPlacement() == JTabbedPane.TOP	|| source.getTabPlacement() == JTabbedPane.BOTTOM) {
			source.initTargetLeftRightLine(source.getTargetTabIndex(e.getLocation()), data);
		} else {
			source.initTargetTopBottomLine(source.getTargetTabIndex(e.getLocation()), data);
		} // if-else

		source.repaint();
		if (source.hasGhost()) {
			source.getS_glassPane().setPoint(source.buildGhostLocation(e.getLocation()));
			source.getS_glassPane().repaint();
		}
	}

	public void drop(DropTargetDropEvent a_event) {
		// System.out.println("DropTarget.drop: " + DnDTabbedPane.this);

		if (isDropAcceptable(a_event)) {
			source.convertTab(source.getTabTransferData(a_event), source.getTargetTabIndex(a_event.getLocation()));
			a_event.dropComplete(true);
		} else {
			a_event.dropComplete(false);
		} // if-else

		source.setDrawRect(false);
		source.repaint();
	}

	public boolean isDragAcceptable(DropTargetDragEvent e) {
		Transferable t = e.getTransferable();
		if (t == null) {
			return false;
		} // if

		DataFlavor[] flavor = e.getCurrentDataFlavors();
		if (!t.isDataFlavorSupported(flavor[0])) {
			return false;
		} // if

		TabTransferData data = source.getTabTransferData(e);

		if (source == data.getTabbedPane()
				&& data.getTabIndex() >= 0) {
			return true;
		} // if

		if (source != data.getTabbedPane()) {
			
			if (source.getTabAcceptor() != null) {
				return source.getTabAcceptor().isDropAcceptable(data.getTabbedPane(),
						data.getTabIndex());
			} // if
		} // if

		return false;
	}

	public boolean isDropAcceptable(DropTargetDropEvent e) {
		Transferable t = e.getTransferable();
		if (t == null) {
			return false;
		} // if

		DataFlavor[] flavor = e.getCurrentDataFlavors();
		if (!t.isDataFlavorSupported(flavor[0])) {
			return false;
		} // if

		TabTransferData data = source.getTabTransferData(e);

		if (source == data.getTabbedPane()	&& data.getTabIndex() >= 0) {
			return true;
		} // if

		if (source != data.getTabbedPane()) {
			if (source.getTabAcceptor() != null) {
				return source.getTabAcceptor().isDropAcceptable(data.getTabbedPane(), data.getTabIndex());
			} // if
		} // if

		return false;
	}
}
